package com.wushi.lenovo.asia5b.app_config;

import com.mvp.base.model.ResultModel;
import com.mvp.dagger.modules.NetworkModule;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.CountModel;
import com.wushi.lenovo.asia5b.app_model.other.CountType;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/11/28.
 * 用户相关静态数据
 */
public class WishRepository {
    private final static WishRepository wishRepository= new WishRepository();

    private final ApiService apiService;
    private int wishCount = 0;//收藏数量
    private Subject<Integer> wishCountSubject;
    private long lastGetWishCountTime = 0;

    private static synchronized WishRepository getInstance() {
        return wishRepository;
    }

    public WishRepository() {
        this.apiService = NetworkModule.getRetrofit().create(ApiService.class);
        wishCountSubject = PublishSubject.create();
    }

    /**
     * 从后台更新数量并通知所有界面
     */
    public static void updateWishCount() {
        getInstance().getWishCountByNet();
    }

    public static void setWishCount(int wishCount) {
        getInstance().wishCount = wishCount;
        getInstance().wishCountSubject.onNext(wishCount);
    }

    /**
     * 订阅数据监听
     *
     * @return
     */
    public static Flowable<Integer> getWishCountOB() {
        return getInstance().wishCountSubject.toFlowable(BackpressureStrategy.LATEST);
    }

    public static int getWishCount() {
        return getInstance().wishCount;
    }


    /**
     * 获取 代付款数
     */
    private void getWishCountByNet() {


        if (!UserRepository.isLogin()) {
            d("--->>>>>>>>---没登录");
            return;
        }
        if ((System.currentTimeMillis() - lastGetWishCountTime) < 1000) {
            return;
        }
        lastGetWishCountTime = System.currentTimeMillis();
        apiService.getCount(CountType.Wish.getCountPath(), UserInfo.getMemberId())
                .map(new Function<ResultModel<CountModel>, Integer>() {
                    @Override
                    public Integer apply(ResultModel<CountModel> countModelResultModel) throws Exception {
                        return countModelResultModel.getData().getCount();
                    }
                })
                .subscribeOn(Schedulers.io())
                .subscribe(total -> {
                    wishCount = total;
                    //通知所有界面
                    if (!UserRepository.isLogin()) {
                        wishCount = 0;
                    }
                    wishCountSubject.onNext(wishCount);
                }, throwable -> {
                    throwable.printStackTrace();
                });

    }
}
